<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Serialization support for Django models &mdash; django-serializable v0.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '#',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="django-serializable v0.1 documentation" href="#" />
    <link rel="next" title="Natural keys" href="doc_pages/natural_keys.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="doc_pages/natural_keys.html" title="Natural keys"
             accesskey="N">next</a> |</li>
        <li><a href="#">django-serializable v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="serialization-support-for-django-models">
<h1>Serialization support for Django models<a class="headerlink" href="#serialization-support-for-django-models" title="Permalink to this headline">¶</a></h1>
<div class="section" id="features">
<h2>Features<a class="headerlink" href="#features" title="Permalink to this headline">¶</a></h2>
<p>This package allows you to build serializable models, that simply work like this :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">parrot</span><span class="o">.</span><span class="n">serialize</span><span class="p">()</span>
<span class="go">{&#39;name&#39;: u&#39;John&#39;, &#39;lastname&#39;: u&#39;TheParrot&#39;, &#39;pk&#39;: 56}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">new_parrot</span> <span class="o">=</span> <span class="n">Parrot</span><span class="o">.</span><span class="n">deserialize</span><span class="p">({</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;Seb&#39;</span><span class="p">,</span> <span class="s">&#39;lastname&#39;</span><span class="p">:</span> <span class="s">&#39;TheKakatoes&#39;</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">new_parrot</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
</pre></div>
</div>
<p>It supports serializing / deserializing :</p>
<blockquote>
<ul class="simple">
<li>foreign keys as natural keys</li>
<li>properties</li>
<li>&#8216;nested&#8217; foreign keys as natural keys</li>
</ul>
</blockquote>
<p>It also provides some generic views for sending and receiving serialized objects :</p>
<blockquote>
<ul class="simple">
<li>getting / creating / updating / deleting an object by its primary key or its natural key</li>
<li>getting object lists, filtered or not</li>
</ul>
</blockquote>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For now the views support only pickle and json format. But as objects are serialized to python dictionnaries, supporting other formats is a 15 minutes fix.</p>
</div>
</div>
<div class="section" id="quick-start">
<h2>Quick start<a class="headerlink" href="#quick-start" title="Permalink to this headline">¶</a></h2>
<ol class="arabic">
<li><p class="first">Put <tt class="xref docutils literal"><span class="pre">serializable</span></tt> to your python path</p>
</li>
<li><p class="first">Create a serializable model :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">serializable.models</span> <span class="kn">import</span> <span class="n">Serializable</span>

<span class="k">class</span> <span class="nc">Author</span> <span class="p">(</span><span class="n">Serializable</span><span class="p">)</span> <span class="p">:</span>
    <span class="c"># SERIALIZABLE</span>
    <span class="n">serialize_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;firstname&quot;</span><span class="p">,</span> <span class="s">&quot;lastname&quot;</span><span class="p">,</span> <span class="s">&quot;pk&quot;</span><span class="p">]</span>
    <span class="n">deserialize_fields</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;firstname&quot;</span><span class="p">,</span> <span class="s">&quot;lastname&quot;</span><span class="p">]</span>
    <span class="c"># NATURAL KEY</span>
    <span class="n">nk_field_names</span> <span class="o">=</span> <span class="p">(</span><span class="s">&quot;firstname&quot;</span><span class="p">,</span> <span class="s">&quot;lastname&quot;</span><span class="p">)</span>
    <span class="c"># FIELDS</span>
    <span class="n">firstname</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">lastname</span> <span class="o">=</span> <span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Connect a serializable view in your urlconf :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="kn">import</span> <span class="o">*</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Author</span>
<span class="kn">from</span> <span class="nn">serializable.views</span> <span class="kn">import</span> <span class="n">get_or_post_serializable</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
        <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^author/(?P&lt;firstname&gt;\w+)/(?P&lt;lastname&gt;\w+)/$&#39;</span><span class="p">,</span> <span class="n">get_or_post_serializable</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s">&quot;model&quot;</span><span class="p">:</span> <span class="n">Author</span><span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">That&#8217;s it !!! Now POST or GET to the connected url :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;/author/John/Steinbeck/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">content</span> <span class="c">#&#39;c&#39; is a HTTP client,</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pickle</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
<span class="go">{&#39;firstname&#39;: &#39;John&#39;, &#39;lastname&#39;: &#39;Steinbeck&#39;, &#39;pk&#39;: 68}</span>
</pre></div>
</div>
</li>
</ol>
</div>
<div class="section" id="documentation-summary">
<h2>Documentation summary<a class="headerlink" href="#documentation-summary" title="Permalink to this headline">¶</a></h2>
<ul>
<li class="toctree-l1"><a class="reference external" href="doc_pages/natural_keys.html">Natural keys</a></li>
<li class="toctree-l1"><a class="reference external" href="doc_pages/models.html">Writing serializable models</a><ul>
<li class="toctree-l2"><a class="reference external" href="doc_pages/models.html#general-case">General case</a></li>
<li class="toctree-l2"><a class="reference external" href="doc_pages/models.html#with-a-single-field-as-natural-key">With a single field as natural key</a></li>
<li class="toctree-l2"><a class="reference external" href="doc_pages/models.html#nested-relationships-deep-serialization">Nested relationships / deep serialization</a></li>
<li class="toctree-l2"><a class="reference external" href="doc_pages/models.html#serializing-deserializing-properties">Serializing/deserializing properties</a></li>
<li class="toctree-l2"><a class="reference external" href="doc_pages/models.html#using-mixins-instead-of-serializable-as-a-base-class">Using mixins instead of <tt class="docutils literal"><span class="pre">Serializable</span></tt> as a base class</a></li>
<li class="toctree-l2"><a class="reference external" href="doc_pages/models.html#module-serializable.models">Package members &#8211; <tt class="docutils literal"><span class="pre">serializable.models</span></tt></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference external" href="doc_pages/views.html">Using generic views for your serializable models</a></li>
</ul>
</div>
<div class="section" id="check-also">
<h2>Check also<a class="headerlink" href="#check-also" title="Permalink to this headline">¶</a></h2>
<p>The &#8216;tests&#8217; folder contains a nice test application that can help you understand how things work.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="#">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">Serialization support for Django models</a><ul>
<li><a class="reference external" href="#features">Features</a></li>
<li><a class="reference external" href="#quick-start">Quick start</a></li>
<li><a class="reference external" href="#documentation-summary">Documentation summary</a><ul>
</ul>
</li>
<li><a class="reference external" href="#check-also">Check also</a></li>
</ul>
</li>
</ul>

            <h4>Next topic</h4>
            <p class="topless"><a href="doc_pages/natural_keys.html"
                                  title="next chapter">Natural keys</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/index.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="doc_pages/natural_keys.html" title="Natural keys"
             >next</a> |</li>
        <li><a href="#">django-serializable v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2010, Sebastien Piquemal.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
    </div>
  </body>
</html>